package org.herrdommel.webpagechecker.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.herrdommel.webpagechecker.pages.AbstractPage;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import android.os.AsyncTask;

public class DownloadPageTask extends AsyncTask<String, Void, TagNode> {

	private final AbstractPage fragment;

	public DownloadPageTask(final AbstractPage f) {
		fragment = f;
	}

	@Override
	protected void onPostExecute(final TagNode result) {
		fragment.onRadarLoaded(result);
	}

	@Override
	protected void onPreExecute() {
		// Do nothing here
	}

	@Override
	protected TagNode doInBackground(final String... params) {
		final HtmlCleaner cleaner = new HtmlCleaner();
		TagNode result = null;
		try {
			Logger.log("HtmlCleaner is cleaning a page...");
			Logger.log("Url: " + params[0]);
			result = cleaner.clean(new URL(params[0]));
			Logger.log("Page cleaned.");
		} catch (final MalformedURLException mue) {
			Logger.log("Wrong URL for page");
		} catch (final IOException ioe) {
			Logger.log("IOException occured while trying to get page content for " + params[0]);
		}
		return result;
	}

}
